<?php
namespace app\admin\common\model;


use think\Db;
use think\Model;

class Role extends Model
{
    protected $pk = 'id';
    protected $table = 'qi_role';

    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    protected $dateFormat = 'Y-m-d H:i:s';



    public function getInfoById($id)
    {
        $data = Role::where('id',$id)->find();
        return $data;
    }

    public function getPageList($pageSize=10)
    {
        $list = Role::order('id','desc')->paginate($pageSize);
        return $list;
    }

    /*
     * 查询指定角色下面的菜单
     */
    public function getRoleMenuById($role_id)
    {
        $roleMenu = Db::table('qi_role_menu')->where('role_id',$role_id)->select();

        $menuIds = [];
        foreach ($roleMenu as $item) {
            $menuIds[] = $item['menu_id'];
        }
        if (empty($menuIds)) {
            return [];
        }

        $menuDatas = Db::table('qi_menu')->where('id','in',$menuIds)->select();
        $menuNames = [];
        foreach ($menuDatas as $item) {
            $menuNames[] = $item['name'];
        }

        return $menuNames;
    }

    /*
     * 查询指定角色下面的用户
     */
    public function getRoleAdminById($role_id)
    {
        $roleUser = Db::table('qi_role_admin')->where('role_id',$role_id)->select();

        $userIds = [];
        foreach ($roleUser as $item) {
            $userIds[] = $item['user_id'];
        }
        if (empty($userIds)) {
            return [];
        }

        $userDatas = Db::table('qi_admin')->where('id','in',$userIds)->select();
        $userNames = [];
        foreach ($userDatas as $item) {
            $userNames[] = $item['username'];
        }

        return $userNames;
    }

    /*
     * 添加角色
     */
    public function addRole($info)
    {
        $result = Role::create($info);
        return $result;
    }

    /*
     * 删除角色
     * 将关联的数据表中的数据都删除
     */
    public function delRole($id)
    {
        Role::where('id',$id)->delete();
        Db::table('qi_role_menu')->where('role_id',$id)->delete();
        Db::table('qi_role_admin')->where('role_id',$id)->delete();

        return true;
    }

    /*
     * 获取角色关联菜单
     */
    public function getRoleMenu($role_id)
    {
        $roleMenu = Db::table('qi_role_menu')->where('role_id',$role_id)->select();

        $menuIds = [];
        foreach ($roleMenu as $item) {
            $menuIds[] = $item['menu_id'];
        }

        return $menuIds;
    }

    /*
     * 更新角色关联菜单
     */
    public function updateRoleMenu($role_id,$menu_ids)
    {
        Db::table('qi_role_menu')->where('role_id',$role_id)->delete();

        $insertDatas = [];
        foreach ($menu_ids as $item) {
            $insertDatas[] = [
              'role_id'=>$role_id,
              'menu_id'=>$item,
            ];
        }

        if ($insertDatas) {
            Db::table('qi_role_menu')->insertAll($insertDatas);
        }
    }

    /*
     * 获取角色关联用户
     */
    public function getRoleAdmin($role_id)
    {
        $roleUser = Db::table('qi_role_admin')->where('role_id',$role_id)->select();

        $userIds = [];
        foreach ($roleUser as $item) {
            $userIds[] = $item['user_id'];
        }

        return $userIds;
    }

    /*
     * 更新角色关联用户
     */
    public function updateRoleAdmin($role_id,$user_ids)
    {
        Db::table('qi_role_admin')->where('role_id',$role_id)->delete();

        $insertDatas = [];
        foreach ($user_ids as $item) {
            $insertDatas[] = [
              'role_id'=>$role_id,
              'user_id'=>$item,
            ];
        }

        if ($insertDatas) {
            Db::table('qi_role_admin')->insertAll($insertDatas);
        }
    }
}